import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'
import { generateMenu } from '@/utils/router'
import { ref } from 'vue'

const router = createRouter({
  history: createWebHashHistory('/'),
  routes: [
    {
      path: '/',
      name: 'index',
      component: () => import('@/layout/index.vue'),
      redirect: '/home/home',
      children: [
        // {
        //   path: '/home/home',
        //   name: 'home',
        //   meta: {
        //     title: '首页'
        //   },
        //   component: () => import('@/views/home/home.vue'),
        // },
      ]
    },
  ]
})

const registerRoute = ref(true)
router.beforeEach(async (to, from, next) => {
  if (registerRoute.value) {
    await generateMenu()
    registerRoute.value = false
    // todo:必须传to，不然刷新后页面空白
    next(to)
  } else {
    next()
  }

})

export default router
